package org.csu.group4.jpetstore.controller;

import org.csu.group4.jpetstore.mybatis.entity.*;
import org.csu.group4.jpetstore.result.Result;
import org.csu.group4.jpetstore.result.ResultFactory;
import org.csu.group4.jpetstore.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BackEndController {
    @Autowired
    SignOnService signOnService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    ProductService productService;

    @Autowired
    ItemService itemService;

    @Autowired
    OrderService orderService;


    @GetMapping(value = "/token")
    public ModelAndView getAdminLoginPage(){
        return new ModelAndView("backend/loginPage");
    }

    @ResponseBody
    @PostMapping(value = "/token")
    public Result login(@RequestBody SignOn signOn){
        if (signOn.getUsername().equals("admin")&&signOn.getPassword().equals("admin")){
            HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                    .getRequest().getSession();
            session.setAttribute("adminUser",signOn.getUsername());
            return ResultFactory.getFailResult("登录成功");
        }else {
            return ResultFactory.getFailResult("用户名或者密码错误");
        }
    }
    @GetMapping(value = "/user")
    public ModelAndView userPage(){
        ModelAndView modelAndView = new ModelAndView("backend/user");
        modelAndView.addObject("users",signOnService.getAllAccount());
        return modelAndView;
    }
    @GetMapping(value = "/getUser")
    @ResponseBody
    public List<Account> getAllUser(){
        return signOnService.getAllAccount();
    }

    @GetMapping(value = "/categories")
    public ModelAndView getAllCategories(){
        ModelAndView modelAndView = new ModelAndView("backend/category");
        List <Category> categories = categoryService.getAll();
        modelAndView.addObject("categories",categories);
        return modelAndView;
    }

    @ResponseBody
    @GetMapping(value = "/products/{categoryId}")
    public ModelAndView productPage(@PathVariable String categoryId){
        ModelAndView modelAndView = new ModelAndView("backend/product");
        Category category = categoryService.getById(categoryId);
        List<Product> products = productService.getAllProductByCategory(category);
        System.out.println(products);
        modelAndView.addObject("products",products);
        return modelAndView;
    }

    @ResponseBody
    @GetMapping(value = "/items/{productId}")
    public ModelAndView itemPage(@PathVariable String productId){
        ModelAndView modelAndView = new ModelAndView("backend/items");
        List <Item> items = itemService.getItems(productId);
        modelAndView.addObject("items",items);
        return modelAndView;
    }

    @ResponseBody
    @GetMapping(value = "/item/{itemId}")
    public ModelAndView item(@PathVariable String itemId){
        ModelAndView modelAndView = new ModelAndView("backend/item");
        Item item = itemService.getItem(itemId);
        Product product = productService.getByProductId(item.getProductId());
        modelAndView.addObject("item",itemService.getItem(itemId));
        modelAndView.addObject("product",product);
        return modelAndView;
    }

    @ResponseBody
    @PostMapping(value = "/item/status")
    public Result setItemStatus(@RequestBody String itemId){
        itemId = itemId.replaceAll("[\"|=]", "");
        itemService.setItemStatus(itemId);
        System.out.println(itemId);
        return ResultFactory.getSuccessResult("item状态改变");
    }

    @ResponseBody
    @GetMapping(value = "/orders")
    public ModelAndView getAllOrders(){
        ModelAndView modelAndView = new ModelAndView("backend/order");
        List <Orders> orders = orderService.getAll();
        modelAndView.addObject("orders",orders);
        return modelAndView;
    }

    @ResponseBody
    @GetMapping(value = "/orders/{orderId}")
    public ModelAndView viewOrder(@PathVariable String orderId){
        ModelAndView modelAndView = new ModelAndView("order/ViewOrder");
        System.out.println(orderId);
        Orders order = orderService.getOrderByOrderId(orderId);
        modelAndView.addObject("order",order);
        return modelAndView;
    }

}
